package sourceagile.server.subversionFileRepository;

import java.io.ByteArrayOutputStream;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNProperties;
import org.tmatesoft.svn.core.io.SVNRepository;

/**
 * 
 * @Feature
 */
public class GetSubversionFileContent {

	public static JSONObject jsonFileContent(String repositoryURL,
			String username, String password, String filePath) {

		return jsonFileContent(getContent(repositoryURL, username, password,
				filePath));
	}

	public static JSONObject jsonFileContent(String fileContent) {

		JSONObject entry = new JSONObject();

		try {

			entry.put("fileContent", fileContent);

		} catch (JSONException e) {

			e.printStackTrace();
		}

		return entry;
	}

	public static String getContent(String repositoryURL, String username,
			String password, String filePath) {

		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		try {

			SVNRepository repository = SubversionRepositoryConnection.connect(
					repositoryURL, username, password);

			SVNProperties fileProperties = new SVNProperties();

			repository.getFile(filePath, -1, fileProperties, baos);

		} catch (SVNException e) {

			e.printStackTrace();
		}

		return baos.toString();
	}

}
